﻿using System.Collections.Generic;
using System.Web.Mvc;
using Blog.Models;
using Business;
using DAL;

namespace Blog.Controllers
{
    public class CommentsController : Controller
    {
        //
        // GET: /Comments/

        public ActionResult Index()
        {
            List<Comment> comments = new List<Comment>();
            comments = CommentsManager.Read();

            return View(comments);
        }

        //
        // GET: /Comments/Details/5

        public ActionResult Details(int id)
        {
            Comment comment = new Comment();
            comment = CommentsManager.Read(id);

            return View(comment);
        }

        //
        // GET: /Comments/Create

        public ActionResult Create()
        {
            string url = Request.ServerVariables["HTTP_REFERER"];
            string[] urlArray = url.Split('/');

            return RedirectToAction("Details/" + urlArray[5], "Articles");
        } 

        //
        // POST: /Comments/Create

        [CaptchaValidator]
        [HttpPost]
        public ActionResult Create(ArticleDetailsViewModel articleVM, bool captchaValid)
        {
            CaptchaValidatorAttribute.ErrorMessage = null;

            if (ModelState.IsValid && captchaValid)
            {
                Comment comment = new Comment();
                comment.Username = articleVM.Username;
                comment.Content = articleVM.Content;
                comment.ArticleID = articleVM.ArticleID;
                CaptchaValidatorAttribute.ErrorMessage = null;

                CommentsManager.Create(comment);
                string url = Request.ServerVariables["HTTP_REFERER"];
                string[] urlArray = url.Split('/');

                return RedirectToAction("Details/" + urlArray[5], "Articles");
            }

            if (!captchaValid)
            {
                CaptchaValidatorAttribute.ErrorMessage = "Les caractères ne correspondent pas à l'image.";
                string url = Request.ServerVariables["HTTP_REFERER"];
                string[] urlArray = url.Split('/');

                return RedirectToAction("Details/" + urlArray[5], "Articles");
            }
            else
            {
                CaptchaValidatorAttribute.ErrorMessage = "Les champs ne sont pas valides.";
                string url = Request.ServerVariables["HTTP_REFERER"];
                string[] urlArray = url.Split('/');

                return RedirectToAction("Details/" + urlArray[5], "Articles");
            }
        }


        //
        // GET: /Comments/Edit/5
        
        [Authorize]
        public ActionResult Edit(int id)
        {
            Comment comment = new Comment();
            comment = CommentsManager.Read(id);

            return View(comment);
        }

        //
        // POST: /Comments/Edit/5

        [Authorize]
        [HttpPost]
        public ActionResult Edit(Comment comment)
        {
            if (ModelState.IsValid)
            {
                CommentsManager.Update(comment);
    
                return RedirectToAction("Index");
            }
            else
                return View();
        }

        //
        // GET: /Comments/Delete/5
        
        [Authorize]
        public ActionResult Delete(int id)
        {
            CommentsManager.Delete(id);
            string url = Request.ServerVariables["HTTP_REFERER"];
            string[] urlArray = url.Split('/');

            return RedirectToAction("Details/" + urlArray[5], "Articles");
        }
    }
}
